/*
 * Copyright 2018 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tomcat.myapplication.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.tomcat.myapplication.http.HttpFactory;
import com.tomcat.myapplication.http.constants.ConstantHttpString;

import java.util.ArrayList;
import java.util.List;

import androidx.multidex.MultiDex;


/**
 * ================================================
 * 本框架核心原理来自于 <a href="https://mp.weixin.qq.com/s/d9QCoBP6kV9VSWvVldVVwA">今日头条官方适配方案</a>
 * <p>
 * 本框架源码的注释都很详细, 欢迎阅读学习
 * <p>
 * AndroidAutoSize 会在 APP 启动时自动完成初始化, 如果您想设置自定义参数可以在 {@link Application#onCreate()} 中设置
 * <p>
 * Created by JessYan on 2018/8/9 17:05
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class BaseApplication extends Application {

    public HttpFactory mHttpFactory;
    public String mType;
    public static List<Activity> activities = new ArrayList<Activity>();
    private static Context context;
//    public  static boolean consumeValue=true;
    @Override
    public void onCreate() {
        super.onCreate();

        //初始化网络
        initHttpFactory(ConstantHttpString.Code_Data);
        context = getApplicationContext();
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    public static Context getContext() {
        return context;
    }




    /**
     * 初始化网络 对像HttpFactory
     */
    public void initHttpFactory(String type) {
        this.mType = type;
        if (mHttpFactory == null) {
            mHttpFactory = HttpFactory.getInstance();
            mHttpFactory.init(getApplicationContext(), type);
        } else {
            if (mType != type) {
                mHttpFactory = HttpFactory.getInstance();
                mHttpFactory.init(getApplicationContext(), type);
            }
        }
    }

    /**
     * 记录activity
     * @param activity
     */
    public static void addActivity(Activity activity) {
        if (!activities.contains(activity)) {
            activities.add(activity);
        }
    }

    /**
     * 关闭activity
     * @param clazz
     */
    public static void finishAllActivity(Class clazz) {
        if (clazz == null) {
            for (Activity item : activities) {
                item.finish();
            }
            activities.clear();
        } else {
            for (Activity item : activities) {
                if (item.getClass() != clazz) {
                    item.finish();
                }
            }
        }
    }

    /**
     * 关闭指定activity
     * @param clazz
     */
    public static void finishActivity(Class clazz){
        for (Activity item : activities) {
            if (item.getClass() == clazz) {
                activities.remove(item);
                item.finish();
            }
        }
    }
}
